package net.zhnb.dao.impl;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

import net.zhnb.bean.Tag;
import net.zhnb.dao.TagDao;
import finaler.util.Page;
import finaler.util.PageList;
import finaler.util.SQLUtil;

public class TagDaoImpl extends MiddleAbstractDao<Tag> implements TagDao {

	@Override
	protected Class<Tag> getEntityClass() {
		return Tag.class;
	}

	@Override
	public PageList getPageList(Page page) throws Exception {
		StringBuffer whereSql = new StringBuffer();
		whereSql.append(" where 1 = 1");
		if(page.getParameter("showerid")!=null&&!page.getParameter("showerid").equals("")){
			whereSql.append(" and b.id = "+page.getParameter("showerid"));
		}
		if(page.getParameter("lotterylun")!=null&&!page.getParameter("lotterylun").equals("")){
			whereSql.append(" and a.lotterylun = "+page.getParameter("lotterylun"));
		}
		if(page.getParameter("type")!=null&&!page.getParameter("type").equals("")){
			whereSql.append(" and a.type = "+page.getParameter("type"));
		}
		PageList pageList = new PageList(page);//固定
		StringBuffer sqlCount = new StringBuffer();
		sqlCount.append("select count(1) from tag a");
		sqlCount.append(" inner join shower b on a.showerid = b.id ");
		sqlCount.append(" inner join room c on b.roomid = c.id");
		sqlCount.append( whereSql );
		int dataCount = super.getIntBySql( sqlCount.toString() );//取总
		pageList.setTotal(dataCount);//总数
		
		StringBuffer queryListSql = new StringBuffer();
		queryListSql.append(" select a.id,c.roomname,b.showername,a.type,a.lotterylun ");
		queryListSql.append(" from tag a ");
		queryListSql.append(" inner join shower b on a.showerid = b.id ");
		queryListSql.append(" inner join room c on b.roomid = c.id");
		queryListSql.append( whereSql );
		queryListSql.append(" order by c.id,b.id,lotterylun,a.id");
		SQLUtil pageSQL = new SQLUtil(queryListSql);//固定
		pageSQL.setFirstResult( page.getStart() );//固定
		pageSQL.setMaxResults( page.getRange() );//固定
		List<Map<String, Object>> dataList = super.findBySqlToMapList(pageSQL.getSql());//数据集
		pageList.setList(dataList);
		return pageList;
	}

	@Override
	public boolean deleteByShowerId(BigDecimal showerId) throws Exception {
		String sqlString = "delete from tag where showerid = " +showerId;
		return super.executeSql(sqlString);
	}

}
